<template>
	<view class="login">
		<view class="title">
			<view class="login_text active">
				登录
				<text class="active"></text>
			</view>
			<view class="register_text " @click="toRegister">
				注册
			</view>

		</view>
		<view class="login_box">
			<view class="username">
				<text>用户名</text>
				<input type="text" placeholder="请输入用户名" v-model="username" />
			</view>
			<view class="password">
				<text>密码</text>
				<input type="password" placeholder="请输入登录密码" v-model="password" />
			</view>
			<view class="tips" @click="toPage('./forget')">
				<text>忘记密码?</text>
			</view>
			<button class="btn" @click="login" :loading="loading" :disabled="loading">
				登录
			</button>
			<view class="wxlogin">
				—— <image src="/static/index/wx.png" mode="widthFix" @click="wxlogin"></image> ——
			</view>

		</view>
	</view>
</template>

<script>
	import {
		userLogin,
		gzLogin
	} from "@/api/api.js"
	export default {
		data() {
			return {
				username: '',
				password: '',
				loading: false
			}
		},
		onLoad(e) {
			if (e.code) {
				gzLogin({
					code: e.code
				}).then(res => {
					uni.$showMsg(res.msg)
					if (res.code == 0) {
						uni.setStorageSync('tokens', res.data.token)
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/user/index'
							})
						}, 500)
					}
				})
			}
		},
		methods: {
			wxlogin() {
				// #ifdef H5
				let appid = this.$config.WXAPPID;
				let redirectUrl = location.href;
				let url =
					`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(redirectUrl)}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`;
				window.location.href = url
				// #endif
			},
			login() {
				if (this.username == '') {
					return uni.$showMsg('请输入用户名')
				}
				if (this.password == '') {
					return uni.$showMsg('请输入密码')
				}
				this.loading = true
				userLogin({
					username: this.username,
					password: this.password
				}).then(res => {
					this.loading = false
					if (res.code == 0) {
						uni.setStorageSync('tokens', res.data.token)
						uni.switchTab({
							url: '/pages/user/index'
						})
					} else {
						uni.$showMsg(res.msg)
					}
				})
			},
			toRegister() {
				uni.redirectTo({
					url: '/pages/register/index'
				})
			}
		}
	}
</script>

<style lang="scss">
	.login {
		padding-top: 20px;
		font-size: 16px;
		height: calc(100vh - 64px);
		background: #1d3f2b;

		.title {
			display: flex;
			justify-content: space-around;
			align-items: center;
			color: #fff;
			width: 50%;
			margin: 0 auto;

			view {
				display: flex;
				flex-direction: column;
				align-items: center;
			}

			text {
				display: block;
				width: 30px;
				height: 3px;
				background: #fff;
				margin-top: 3px;
			}
		}

		.active {
			font-size: 24px;
		}

		.login_box {
			background: #fff;
			width: 80%;
			padding: 5%;
			padding-top: 10%;
			border-radius: 8px;
			margin: 20px auto;

			text {
				margin: 10px 0;
				display: block;
				font-weight: bold;
				font-size: 14px;
			}

			input {
				border: 1px solid #377751;
				outline: 0;
				border-radius: 5px;
				height: 40px;
				padding-left: 15px;
				margin-top: 10px;
			}
		}

		.wxlogin {
			text-align: center;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #ccc;

			image {
				width: 30px;
				margin: 0 10px;
			}
		}

		.tips {
			display: flex;
			justify-content: flex-end;
			font-size: 14px;
			align-items: center;

			text {
				font-weight: normal;
				color: #777;
			}

			.forget {
				color: $uni-text-color;
			}
		}


	}
</style>